package config

import (
    "fmt"
    "os"

    "gopkg.in/yaml.v3"
)

// Config 全局配置
type Config struct {
    Database struct {
        Host     string `yaml:"host"`
        Port     int    `yaml:"port"`
        User     string `yaml:"user"`
        Password string `yaml:"password"`
        Name     string `yaml:"name"`
    } `yaml:"database"`
    
    Redis struct {
        Host     string `yaml:"host"`
        Port     int    `yaml:"port"`
        Password string `yaml:"password"`
        DB       int    `yaml:"db"`
    } `yaml:"redis"`
    
    Server struct {
        LoginPort int `yaml:"login_port"`
        WorldPort int `yaml:"world_port"`
        BattlePort int `yaml:"battle_port"`
        SocialPort int `yaml:"social_port"`
    } `yaml:"server"`
    
    Game struct {
        Name    string `yaml:"name"`
        Version string `yaml:"version"`
        MaxPlayers int `yaml:"max_players"`
    } `yaml:"game"`
}

// 全局配置实例
var GlobalConfig Config

// LoadConfig 从文件加载配置
func LoadConfig(filePath string) error {
    file, err := os.ReadFile(filePath)
    if err != nil {
        return fmt.Errorf("failed to read config file: %v", err)
    }
    
    err = yaml.Unmarshal(file, &GlobalConfig)
    if err != nil {
        return fmt.Errorf("failed to parse config file: %v", err)
    }
    
    return nil
}    